package com.point.common.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * The class Big decimal util.
 *
 * @author admin
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class BigDecimalUtil {

    /**
     * Add big decimal.
     *
     * @param v1 the v 1
     * @param v2 the v 2
     * @return the big decimal
     */
    public static BigDecimal add(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2);
    }

    /**
     * Sub big decimal.
     *
     * @param v1 the v 1
     * @param v2 the v 2
     * @return the big decimal
     */
    public static BigDecimal subtract(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2);
    }


    /**
     * Mul big decimal.
     *
     * @param v1 the v 1
     * @param v2 the v 2
     * @return the big decimal
     */
    public static BigDecimal multiply(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2);
    }

    /**
     * Div big decimal.
     *
     * @param v1 the v 1
     * @param v2 the v 2
     * @return the big decimal
     */
    public static BigDecimal divide(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP);
    }


    /**
     * 保留两位小数
     *
     * @param amount
     * @return
     */
    public static String getTwoDecimal(BigDecimal amount) {
        if (null == amount) {
            return "";
        }
        return amount.setScale(2, BigDecimal.ROUND_HALF_UP).toString();

    }

    /**
     * 去掉小数点后无用的0
     *
     * @param amount
     * @return
     */
    public static String removeZero(BigDecimal amount) {
        if (null == amount) {
            return "";
        }
        return amount.stripTrailingZeros().toPlainString();

    }

    /**
     * 转成百分比
     *
     * @param amount
     * @return
     */
    public static String getPercent(BigDecimal amount) {
        if (null == amount) {
            return "";
        }
        DecimalFormat df = new DecimalFormat("0.00%");
        return df.format(amount);
    }


}
